<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
'use strict';

// When |callback| is invoked, |promise| is resolved. Example:
//   let cp = new CallbackPromise();
//   pc.getStats(cp.callback);
//   let report = await cp.promise;
class CallbackPromise {
  constructor() {
    let promiseResolve;
    this.promise = new Promise(function(resolve, reject) {
      promiseResolve = resolve;
    });
    this.callback = promiseResolve;
  }
}

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let cp = new CallbackPromise();
  assert_equals(await pc.getStats(cp.callback), undefined);
  await cp.promise;
}, 'Signature: getStats(callback) uses callback');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let cp = new CallbackPromise();
  let stream = await navigator.mediaDevices.getUserMedia({video:true});
  let track = stream.getTracks()[0];
  assert_equals(await pc.getStats(cp.callback, track), undefined);
  await cp.promise;
}, 'Signature: getStats(callback, track) uses callback');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let cp = new CallbackPromise();
  assert_equals(await pc.getStats(cp.callback, null), undefined);
  await cp.promise;
}, 'Signature: getStats(callback, null) uses callback');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let cp = new CallbackPromise();
  assert_equals(await pc.getStats(cp.callback, undefined), undefined);
  await cp.promise;
}, 'Signature: getStats(callback, undefined) uses callback');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let cp = new CallbackPromise();
  let nonsense = 5;
  assert_equals(await pc.getStats(cp.callback, nonsense), undefined);
  await cp.promise;
}, 'Signature: getStats(callback, nonsense) uses callback');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let nonsense = 5;
  await promise_rejects_js(t, TypeError, pc.getStats(nonsense));
}, 'Signature: getStats(nonsense) throws exception');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_not_equals(await pc.getStats(), undefined);
}, 'Signature: getStats() resolves with report');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  let stream = await navigator.mediaDevices.getUserMedia({video:true});
  let track = stream.getTracks()[0];
  pc.addTrack(track);
  assert_not_equals(await pc.getStats(track), undefined);
}, 'Signature: getStats(track) resolves with report');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_not_equals(await pc.getStats(null), undefined);
}, 'Signature: getStats(null) resolves with report');

promise_test(async t => {
  let pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_not_equals(await pc.getStats(undefined), undefined);
}, 'Signature: getStats(undefined) resolves with report');
</script>
